package com.wash.shoes.controller.app;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.domain.dto.ShopStsXcxDto;
import com.wash.shoes.domain.vo.ShopStsXcxVo;
import com.wash.shoes.domain.vo.StsAllInfo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.mapper.ShopMapper;
import com.wash.shoes.mapper.UserShopMapper;
import com.wash.shoes.resp.WashShopVo;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.OrderTransportService;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.service.UserShopService;
import com.wash.shoes.util.AdminUserUtil;
import com.wash.shoes.util.StsDateInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lijianguo
 * @Date 26/02/2025 15:47
 * 请输入类的简介
 **/
@Log4j2
@Api(tags = {"APP-统计"})
@RestController
@RequestMapping("/app/sts")
public class StsController {

    @Resource
    OrderService orderService;

    @Resource
    OrderTransportService orderTransportService;
    @Autowired
    private ShopService shopService;

    @Resource
    UserShopService userShopService;

    @ApiOperation("用户能看见的门店")
    @PostMapping("/userAdminShopList")
    public R<List<ShopPo>> userAdminShopList() {
        String id = AdminUserUtil.getUserId();
        List<ShopPo> dataList = null;
        if (!AdminUserUtil.getLoginUser().getUser().hasAdmin()) {
            dataList = userShopService.userAllShopList(id);
        } else {
            dataList = shopService.list(new LambdaQueryWrapper<ShopPo>().eq(ShopPo::getStatus, 1));
        }

        return R.ok(dataList);
    }

    @Autowired
    private UserShopMapper userShopMapperStic;

    @ApiOperation("洗鞋的工厂")
    @PostMapping("/washShopList")
    public R<List<WashShopVo>> washShopList() {
        String id = AdminUserUtil.getUserId();
        List<ShopPo> dataList = Lists.newArrayList();
//        List<String> distinctShopNames = Lists.newArrayList();
        List<WashShopVo> washShopList = Lists.newArrayList();
        if (!AdminUserUtil.getLoginUser().getUser().hasAdmin()) {
            List<String> userBindTypeList = userShopMapperStic.getUserBindType(String.valueOf(AdminUserUtil.getUserId()));
            String userBindType = "";
            if (CollectionUtil.isNotEmpty(userBindTypeList)) {
                userBindType = userBindTypeList.get(0);
            }
            //工厂
            if ("factory".equals(userBindType)) {
                dataList = userShopService.userAllShopList(id);
            } else {
                //取这个用户绑定的工厂
                dataList = shopService.getShopByShopIds();
            }
            washShopList = dataList.stream().map(e -> {
                WashShopVo washShopVo = new WashShopVo();
                washShopVo.setType(0);
                washShopVo.setName(e.getShopName());
                washShopVo.setBizId(e.getId());
                return washShopVo;
            }).collect(Collectors.toList());
        } else {
            washShopList = shopMapper.washShopList();
            if (CollectionUtil.isNotEmpty(washShopList)) {
                washShopList = washShopList.stream()
                        .filter(e -> {
                            return StringUtils.isNotEmpty(e.getName());
                        })
                        .distinct()
                        .collect(Collectors.toList());
            }
        }
        return R.ok(washShopList);
    }

    @Autowired
    private ShopMapper shopMapper;

    @ApiOperation("门店分析-订单金额、到账金额、预估未来到店衣鞋数")
    @PostMapping("/shopMoneySts")
    public R<ShopStsXcxVo> shopMoneySts(@RequestBody ShopStsXcxDto shopStsXcxDto) {
        log.info("shopMoneySts param is {}", JSONObject.toJSONString(shopStsXcxDto));
        StsDateInfoUtil.processDate(shopStsXcxDto);
        Integer count = orderTransportService.incomeOrderNum(shopStsXcxDto);
        ShopStsXcxVo vo = orderService.shopMoneySts(shopStsXcxDto);
        vo.setInComeOrderNo(count);

        return R.ok(vo);
    }


    @ApiOperation("收益分析-订单金额")
    @PostMapping("/orderMoneySts")
    public R<StsAllInfo> orderMoneySts(@RequestBody ShopStsXcxDto shopStsXcxDto) {
        log.info("orderMoneySts param is {}", JSONObject.toJSONString(shopStsXcxDto));
        StsDateInfoUtil.processDate(shopStsXcxDto);
        StsAllInfo dataInfo = orderService.orderMoneySts(shopStsXcxDto);
        return R.ok(dataInfo);
    }

    @ApiOperation("洗鞋工厂分析-订单")
    @PostMapping("/orderWashShopSts")
    public R<StsAllInfo> orderWashShopSts(@RequestBody ShopStsXcxDto shopStsXcxDto) {
        log.info("orderWashShopSts param is {}", JSONObject.toJSONString(shopStsXcxDto));
        StsDateInfoUtil.processDate(shopStsXcxDto);
        StsAllInfo dataInfo = orderService.orderWashShopSts(shopStsXcxDto);
        return R.ok(dataInfo);
    }

    @ApiOperation("洗鞋工厂分析-洗鞋的数量")
    @PostMapping("/orderWashShoeSts")
    public R<StsAllInfo> orderWashShoeShopSts(@RequestBody ShopStsXcxDto shopStsXcxDto) {
        log.info("orderWashShoeSts param is {}", JSONObject.toJSONString(shopStsXcxDto));
        StsDateInfoUtil.processDate(shopStsXcxDto);
        StsAllInfo dataInfo = orderService.orderWashShoeSts(shopStsXcxDto);
        return R.ok(dataInfo);
    }

}
